#region
/* Adoor.NET - Object/Relational mapping framework leveraging ADO.NET`s powerful disconnected mode.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 
#endregion

using System.Diagnostics;
using System.Text;
using Adoor.Expression.AST;

namespace Adoor.Expression
{
    /// <summary>
    /// Visit AST and write processing output to a StringBuilder.
    /// </summary>
	public class ExpressionStringizer: IExpressionVisitor
	{
		public static string Evaluate(ExpressionNode en)
		{
			ExpressionStringizer es = new ExpressionStringizer();
			en.Accept(es);
			return es.Result.ToString();
		}

		public StringBuilder Result = new StringBuilder();

		public virtual void VisitName(ExpressionName n)
		{			
			if (n.Qualifier != "") 
			{
				this.Result.Append(n.Qualifier);
				this.Result.Append('.');
				this.Result.Append(n.Value);
				return;
			}
			this.Result.Append(n.Value);			
		}

		public virtual void VisitEscape(ExpressionEscape e)
		{
			this.Result.Append('{');
			this.Result.Append(e.Content);
			this.Result.Append('}');
		}

		public virtual void VisitParen(ExpressionParen p)
		{
			this.Result.Append('(');
			p.Exp.Accept(this);
			this.Result.Append(')');
		}

		public virtual void VisitPrimary(ExpressionPrimary p)
		{
			this.Result.Append(p.Value);
		}

		public virtual void VisitIs(ExpressionIs i)
		{
			i.Exp.Accept(this);
			this.Result.Append(" is");
			if (i.IsNot)
				this.Result.Append(" not");
			switch (i.What)
			{
				case ScannerToken.Null:
					this.Result.Append(" null");
					break;
				case ScannerToken.True:
					this.Result.Append(" true");
					break;
				case ScannerToken.False:
					this.Result.Append(" false");
					break;
				default:
                    throw new System.NotSupportedException(i.What + " is not supported");					
			}
		}

		public virtual void VisitIn(ExpressionIn j)
		{
			j.Exp.Accept(this);

			this.Result.Append(" in (");

			for (int i = 0; i < j.Nodes.Length; i++)
			{
				if(i != 0)
					this.Result.Append(", ");
				j.Nodes[i].Accept(this);
			}
			this.Result.Append(" )");
		}

		public virtual void VisitInSubQuery(ExpressionInSubquery j)
		{
			throw new System.NotImplementedException();			
		}

		public virtual void VisitExistsSubQuery(ExpressionExistsSubquery j)
		{
			throw new System.NotImplementedException();			
		}

		public virtual void VisitUnary(ExpressionUnary u)
		{
			switch (u.Op)
			{
				case ScannerToken.PlusSign:
					this.Result.Append('+');
					break;
				case ScannerToken.MinusSign:
					this.Result.Append('-');
					break;
				case ScannerToken.BitWiseNot:
					this.Result.Append('~');
					break;
				case ScannerToken.Not:
					this.Result.Append("not ");
					break;
				default:
                    throw new System.NotSupportedException(u.Op + " is not supported");					
					
			}
			u.Exp.Accept(this);
		}

		public virtual void VisitBinary(ExpressionBinary b)
		{
			b.Exp1.Accept(this);

			switch (b.Op)
			{
				case ScannerToken.PlusSign:
					this.Result.Append(" + ");
					break;
				case ScannerToken.MinusSign:
					this.Result.Append(" - ");					
					break;
				case ScannerToken.Asterisk:
					this.Result.Append(" * ");
					break;
				case ScannerToken.Solidus:
					this.Result.Append(" / ");
					break;
				case ScannerToken.Eq:
					this.Result.Append(" = ");
					break;
				case ScannerToken.Ne:
					this.Result.Append(" <> ");
					break;
				case ScannerToken.Gt:
					this.Result.Append(" > ");
					break;
				case ScannerToken.Ge:
					this.Result.Append(" >= ");
					break;
				case ScannerToken.Lt:
					this.Result.Append(" < ");
					break;
				case ScannerToken.Le:
					this.Result.Append(" <= ");
					break;
				case ScannerToken.Or:
					this.Result.Append(" or ");
					break;
				case ScannerToken.And:
					this.Result.Append(" and ");
					break;
				case ScannerToken.Is:
					this.Result.Append(" is ");
					break;
				case ScannerToken.BitWiseAnd:
					this.Result.Append(" & ");
					break;
				case ScannerToken.BitWiseOr:
					this.Result.Append(" | ");
					break;
				case ScannerToken.Modulo:
					this.Result.Append(" % ");
					break;
				default:
                    throw new System.NotSupportedException(b.Op + " is not supported");
			}			
			b.Exp2.Accept(this);
		}


		public virtual void VisitLike(ExpressionLike l)
		{
			l.Exp1.Accept(this);
			if (l.IsNotLike)
				this.Result.Append(" not");
			this.Result.Append(" like ");
			l.Exp2.Accept(this);
		}

		public virtual void VisitFunction(ExpressionFunction f)
		{
			this.Result.Append(f.Name);
			this.Result.Append('(');
			bool comma = false;
			foreach(ExpressionNode p in f.Parameters) 
			{
				if (comma)
					this.Result.Append(", ");
				p.Accept(this);
				comma = true;
			}
			this.Result.Append(')');
		}

		public virtual void VisitCorrelationFilteredNode(ExpressionCorrelationFilteredNode c)
		{
//			foreach(ExpressionNode en in c.Nodes.Values)
//				en.Accept(this);
			c.DefaultNode.Accept(this);
		}
	}
}
